#pragma once

#include <QString>
#include <QDebug>
#include <QDateTime>
#include <QFileInfo>

namespace ns_log
{
enum class LogLevel {
    Info,
    Debug,
    Warning,
    Error,
    Fatal
};


inline QDebug Log(LogLevel level, const QString &file, int line)
{

    QString levelStr;
    switch(level) {
    case LogLevel::Info:    levelStr = "INFO"; break;
    case LogLevel::Debug:   levelStr = "DEBUG"; break;
    case LogLevel::Warning: levelStr = "WARNING"; break;
    case LogLevel::Error:  levelStr = "ERROR"; break;
    case LogLevel::Fatal:  levelStr = "FATAL"; break;
    }

    QString timestamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    // 简化文件名显示（只显示最后一部分）
    QFileInfo fileinfo(file);
    QString simplifiedFile = fileinfo.fileName();

    // QString simplifiedFile = file.mid(file.lastIndexOf('\\') + 1);

    //qDebug默认会打印双引号，加上noquote就没有了
    return qDebug().noquote() << QString("[%1][%2:%3][%4]")
                           .arg(levelStr)
                           .arg(simplifiedFile)
                           .arg(line)
                           .arg(timestamp);
}
}

// 使用宏定义简化调用
#define LOG_INFO    ns_log::Log(ns_log::LogLevel::Info,    __FILE__, __LINE__)
#define LOG_DEBUG   ns_log::Log(ns_log::LogLevel::Debug,   __FILE__, __LINE__)
#define LOG_WARNING ns_log::Log(ns_log::LogLevel::Warning, __FILE__, __LINE__)
#define LOG_ERROR   ns_log::Log(ns_log::LogLevel::Error,   __FILE__, __LINE__)
#define LOG_FATAL   ns_log::Log(ns_log::LogLevel::Fatal,   __FILE__, __LINE__)
